package com.woniu.adopt.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.adopt.entity.Permission;
import com.woniu.adopt.mapper.PermissionMapper;
import com.woniu.adopt.vo.PermissionVo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class PermissionService {

    @Autowired
    PermissionMapper permissionMapper;

    public List<PermissionVo> queryAllMenu(){
        return permissionMapper.selectAllMenu();
    }

    public PageInfo<Permission> queryPermissionByPage(Integer now, Integer size, String name){
        PageHelper.startPage(now,size);
        List<Permission> list = permissionMapper.selectPermissionByName(name);
        return new PageInfo<>(list);
    }

    public List<Permission> queryParentMenu(){

        return permissionMapper.selectParentPermission();
    }

    public int add(Permission permission){
        return permissionMapper.insert(permission);
    }

    public int modify(Permission permission){
        return permissionMapper.updateByPrimaryKey(permission);
    }
    public Permission queryPerByKey(int id){
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Transactional
    public int removePermissionByKey(Integer id){
        //清除 和角色的关系
        int m = permissionMapper.deleteRoleAndPermissionByPerId(id);
        //        //删除权限
        int i = permissionMapper.deleteByPrimaryKey(id);
        return i ;
    }

    /**
     * 根据 角色的id查询 当前角色 拥有哪些权限 （id）
     * @param roleid
     * @return
     */
    public List<Integer> queryPermissionIdByRoleId(int roleid){
        return permissionMapper.selectPermissionIdByRoleId(roleid);
    }

    public List<PermissionVo> queryMenuByManager(int mid) {
        return permissionMapper.selectMenuByManager(mid);
    }
}
